/*
 * Copyright (c) 2022 Hong Jiahua
 * https://gitee.com/arrco/ersh_keyboard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "ersh_keyboard.h"
/************************************************************************/
/*                                                                      */
/************************************************************************/
typedef struct
{
    int id;
    char* name;
} keyboard_name_t;

keyboard_name_t keyboard_name[] = {
    {KEY_UP,        "Up"},
    {KEY_DOWN,      "Down"},
    {KEY_LEFT,      "Left"},
    {KEY_RIGHT,     "Right"},
    
    {KEY_INSERT,    "Insert"},
    {KEY_DELETE,    "Delete"},

    {KEY_HOME,      "Home"},
    {KEY_END,       "End"},

    {KEY_PAGE_UP,   "Page Up"},
    {KEY_PAGE_DOWN, "Page Down"},
    
    {KEY_BACKSPACE, "Backspace"},
    {KEY_ENTER,     "Enter or Ctrl + M"},
    {KEY_ESC,       "Esc"},
    
    {KEY_CTRL_A,    "Ctrl + A"},
    {KEY_CTRL_B,    "Ctrl + B"},
    {KEY_CTRL_C,    "Ctrl + C"},
    {KEY_CTRL_D,    "Ctrl + D"},
    {KEY_CTRL_E,    "Ctrl + E"},
    {KEY_CTRL_F,    "Ctrl + F"},
    {KEY_CTRL_G,    "Ctrl + G"},
    {KEY_CTRL_H,    "Ctrl + H"},
    {KEY_CTRL_I,    "Ctrl + I"},
    {KEY_CTRL_J,    "Ctrl + J  or  Ctrl + Enter"},
    {KEY_CTRL_K,    "Ctrl + K"},
    {KEY_CTRL_L,    "Ctrl + L"},
    {KEY_CTRL_M,    "Ctrl + M  or  Enter"},
    {KEY_CTRL_N,    "Ctrl + N"},
    {KEY_CTRL_O,    "Ctrl + O"},
    {KEY_CTRL_P,    "Ctrl + P"},
    {KEY_CTRL_Q,    "Ctrl + Q"},
    {KEY_CTRL_R,    "Ctrl + R"},
    {KEY_CTRL_S,    "Ctrl + S"},
    {KEY_CTRL_T,    "Ctrl + T"},
    {KEY_CTRL_U,    "Ctrl + U"},
    {KEY_CTRL_V,    "Ctrl + V"},
    {KEY_CTRL_W,    "Ctrl + W"},
    {KEY_CTRL_X,    "Ctrl + X"},
    {KEY_CTRL_Y,    "Ctrl + Y"},
    {KEY_CTRL_Z,    "Ctrl + Z"},
    
    {KEY_CTRL_ENTER,                "Ctrl + Enter"},
    {KEY_CTRL_LEFT_SQUARE_BRACKET,  "Ctrl + ["},
    {KEY_CTRL_BACKSLASH,            "Ctrl + \\"},
    {KEY_CTRL_RIGHT_SQUARE_BRACKET, "Ctrl + ]"},
    {KEY_CTRL_SLASH,                "Ctrl + /"},
    
    {KEY_CTRL_INSERT,               "Ctrl + Insert"},
    {KEY_CTRL_DELETE,               "Ctrl + Delete"},
    
    {KEY_CTRL_HOME,                 "Ctrl + Home"},
    {KEY_CTRL_END,                  "Ctrl + End"},

    {KEY_CTRL_PAGE_UP,              "Ctrl + Page Up"},
    {KEY_CTRL_PAGE_DOWN,            "Ctrl + Page Down"},
    
    {KEY_F1,        "F1" },
    {KEY_F2,        "F2" },
    {KEY_F3,        "F3" },
    {KEY_F4,        "F4" },
    {KEY_F5,        "F5" },
    {KEY_F6,        "F6" },
    {KEY_F7,        "F7" },
    {KEY_F8,        "F8" },
    {KEY_F9,        "F9" },
    {KEY_F10,       "F10"},
    {KEY_F11,       "F11"},
    {KEY_F12,       "F12"},

    {KEY_ALT_CTRL_A,    "Alt + Ctrl + A"},
    {KEY_ALT_CTRL_B,    "Alt + Ctrl + B"},
    {KEY_ALT_CTRL_C,    "Alt + Ctrl + C"},
    {KEY_ALT_CTRL_D,    "Alt + Ctrl + D"},
    {KEY_ALT_CTRL_E,    "Alt + Ctrl + E"},
    {KEY_ALT_CTRL_F,    "Alt + Ctrl + F"},
    {KEY_ALT_CTRL_G,    "Alt + Ctrl + G"},
    {KEY_ALT_CTRL_H,    "Alt + Ctrl + H"},
    {KEY_ALT_CTRL_I,    "Alt + Ctrl + I"},
    {KEY_ALT_CTRL_J,    "Alt + Ctrl + J"},
    {KEY_ALT_CTRL_K,    "Alt + Ctrl + K"},
    {KEY_ALT_CTRL_L,    "Alt + Ctrl + L"},
    {KEY_ALT_CTRL_M,    "Alt + Ctrl + M"},
    {KEY_ALT_CTRL_N,    "Alt + Ctrl + N"},
    {KEY_ALT_CTRL_O,    "Alt + Ctrl + O"},
    {KEY_ALT_CTRL_P,    "Alt + Ctrl + P"},
    {KEY_ALT_CTRL_Q,    "Alt + Ctrl + Q"},
    {KEY_ALT_CTRL_R,    "Alt + Ctrl + R"},
    {KEY_ALT_CTRL_S,    "Alt + Ctrl + S"},
    {KEY_ALT_CTRL_T,    "Alt + Ctrl + T"},
    {KEY_ALT_CTRL_U,    "Alt + Ctrl + U"},
    {KEY_ALT_CTRL_V,    "Alt + Ctrl + V"},
    {KEY_ALT_CTRL_W,    "Alt + Ctrl + W"},
    {KEY_ALT_CTRL_X,    "Alt + Ctrl + X"},
    {KEY_ALT_CTRL_Y,    "Alt + Ctrl + Y"},
    {KEY_ALT_CTRL_Z,    "Alt + Ctrl + Z"},
    
    {KEY_ALT_SPACE,              "Alt + Space"},
    {KEY_ALT_EXCLAMATION_MARK,   "Alt + !"},
    {KEY_ALT_DOUBLE_QUOTE,       "Alt + \""},
    {KEY_ALT_NUMBER,             "Alt + #"},
    {KEY_ALT_DOLLAR ,            "Alt + $"},
    {KEY_ALT_PERCENT,            "Alt + %"},
    {KEY_ALT_AMPERSAND,          "Alt + &"},
    {KEY_ALT_SINGLE_QUOTE,       "Alt + '"},
    {KEY_ALT_LEFT_PARENTHESIS,   "Alt + ("},
    {KEY_ALT_RIGHT_PARENTHESIS,  "Alt + )"},
    {KEY_ALT_ASTERISK,           "Alt + *"},
    {KEY_ALT_PLUS,               "Alt + +"},
    {KEY_ALT_COMMA,              "Alt + ,"},
    {KEY_ALT_MINUS,              "Alt + -"},
    {KEY_ALT_PERIOD,             "Alt + ."},
    {KEY_ALT_SLASH,              "Alt + /"},
    {KEY_ALT_0,                  "Alt + 0"},
    {KEY_ALT_1,                  "Alt + 1"},
    {KEY_ALT_2,                  "Alt + 2"},
    {KEY_ALT_3,                  "Alt + 3"},
    {KEY_ALT_4,                  "Alt + 4"},
    {KEY_ALT_5,                  "Alt + 5"},
    {KEY_ALT_6,                  "Alt + 6"},
    {KEY_ALT_7,                  "Alt + 7"},
    {KEY_ALT_8,                  "Alt + 8"},
    {KEY_ALT_9,                  "Alt + 9"},
    {KEY_ALT_COLON,              "Alt + :"},
    {KEY_ALT_SEMICOLON,          "Alt + ;"},
    {KEY_ALT_LESS_THAN,          "Alt + <"},
    {KEY_ALT_EQUALS,             "Alt + ="},
    {KEY_ALT_GREATER_THAN,       "Alt + >"},
    {KEY_ALT_QUESTION_MARK,      "Alt + ?"},
    {KEY_ALT_AT_SIGN,            "Alt + @"},
    
    {KEY_ALT_A,                  "Alt + A"},
    {KEY_ALT_B,                  "Alt + B"},
    {KEY_ALT_C,                  "Alt + C"},
    {KEY_ALT_D,                  "Alt + D"},
    {KEY_ALT_E,                  "Alt + E"},
    {KEY_ALT_F,                  "Alt + F"},
    {KEY_ALT_G,                  "Alt + G"},
    {KEY_ALT_H,                  "Alt + H"},
    {KEY_ALT_I,                  "Alt + I"},
    {KEY_ALT_J,                  "Alt + J"},
    {KEY_ALT_K,                  "Alt + K"},
    {KEY_ALT_L,                  "Alt + L"},
    {KEY_ALT_M,                  "Alt + M"},
    {KEY_ALT_N,                  "Alt + N"},
    {KEY_ALT_O,                  "Alt + O"},
    {KEY_ALT_P,                  "Alt + P"},
    {KEY_ALT_Q,                  "Alt + Q"},
    {KEY_ALT_R,                  "Alt + R"},
    {KEY_ALT_S,                  "Alt + S"},
    {KEY_ALT_T,                  "Alt + T"},
    {KEY_ALT_U,                  "Alt + U"},
    {KEY_ALT_V,                  "Alt + V"},
    {KEY_ALT_W,                  "Alt + W"},
    {KEY_ALT_X,                  "Alt + X"},
    {KEY_ALT_Y,                  "Alt + Y"},
    {KEY_ALT_Z,                  "Alt + Z"},
    
    {KEY_ALT_LEFT_SQUARE_BRACKET,   "Alt + ["},
    {KEY_ALT_BACKSLASH,             "Alt + \\"},
    {KEY_ALT_RIGHT_SQUARE_BRACKET,  "Alt + ]"},
    {KEY_ALT_CARET ,                "Alt + ^"},
    {KEY_ALT_UNDERSCORE,            "Alt + _"},
    {KEY_ALT_GRAVE_ACCENT,          "Alt + `"},
    
    {KEY_ALT_LOWERCASE_A,       "Alt + a"},
    {KEY_ALT_LOWERCASE_B,       "Alt + b"},
    {KEY_ALT_LOWERCASE_C,       "Alt + c"},
    {KEY_ALT_LOWERCASE_D,       "Alt + d"},
    {KEY_ALT_LOWERCASE_E,       "Alt + e"},
    {KEY_ALT_LOWERCASE_F,       "Alt + f"},
    {KEY_ALT_LOWERCASE_G,       "Alt + g"},
    {KEY_ALT_LOWERCASE_H,       "Alt + h"},
    {KEY_ALT_LOWERCASE_I,       "Alt + i"},
    {KEY_ALT_LOWERCASE_J,       "Alt + j"},
    {KEY_ALT_LOWERCASE_K,       "Alt + k"},
    {KEY_ALT_LOWERCASE_L,       "Alt + l"},
    {KEY_ALT_LOWERCASE_M,       "Alt + m"},
    {KEY_ALT_LOWERCASE_N,       "Alt + n"},
    {KEY_ALT_LOWERCASE_O,       "Alt + o"},
    {KEY_ALT_LOWERCASE_P,       "Alt + p"},
    {KEY_ALT_LOWERCASE_Q,       "Alt + q"},
    {KEY_ALT_LOWERCASE_R,       "Alt + r"},
    {KEY_ALT_LOWERCASE_S,       "Alt + s"},
    {KEY_ALT_LOWERCASE_T,       "Alt + t"},
    {KEY_ALT_LOWERCASE_U,       "Alt + u"},
    {KEY_ALT_LOWERCASE_V,       "Alt + v"},
    {KEY_ALT_LOWERCASE_W,       "Alt + w"},
    {KEY_ALT_LOWERCASE_X,       "Alt + x"},
    {KEY_ALT_LOWERCASE_Y,       "Alt + y"},
    {KEY_ALT_LOWERCASE_Z,       "Alt + z"},
    
    {KEY_ALT_OPENING_BRACE,     "Alt + {"},
    {KEY_ALT_VERTICAL_BAR,      "Alt + |"},
    {KEY_ALT_CLOSING_BRACE,     "Alt + }"},
    
    {KEY_ALT_TILDE,             "Alt + ~"},
    {KEY_ALT_BACKSPACE,         "Alt + Backspace"},
    
    {KEY_ALT_UP,                "Alt + Up   "},
    {KEY_ALT_DOWN,              "Alt + Down "},
    {KEY_ALT_LEFT,              "Alt + Left "},
    {KEY_ALT_RIGHT,             "Alt + Right"},
    {KEY_ALT_INSERT,            "Alt + Insert"},
    {KEY_ALT_DELETE,            "Alt + Delete"},
    {KEY_ALT_HOME,              "Alt + Home"},
    {KEY_ALT_END,               "Alt + End "},
    {KEY_ALT_PAGE_UP,           "Alt + Page Up  "},
    {KEY_ALT_PAGE_DOWN,         "Alt + Page Down"},
    
    {KEY_ALT_F1,                "Alt + F1 "},
    {KEY_ALT_F2,                "Alt + F2 "},
    {KEY_ALT_F3,                "Alt + F3 "},
    {KEY_ALT_F4,                "Alt + F4 "},
    {KEY_ALT_F5,                "Alt + F5 "},
    {KEY_ALT_F6,                "Alt + F6 "},
    {KEY_ALT_F7,                "Alt + F7 "},
    {KEY_ALT_F8,                "Alt + F8 "},
    {KEY_ALT_F9,                "Alt + F9 "},
    {KEY_ALT_F10,               "Alt + F10"},
    {KEY_ALT_F11,               "Alt + F11"},
    {KEY_ALT_F12,               "Alt + F12"},
    
    {KEY_ALT_CTRL_UP,           "Alt + Ctrl + Up   "},
    {KEY_ALT_CTRL_DOWN,         "Alt + Ctrl + Down "},
    {KEY_ALT_CTRL_LEFT,         "Alt + Ctrl + Left "},
    {KEY_ALT_CTRL_RIGHT,        "Alt + Ctrl + Right"},
    {KEY_ALT_CTRL_INSERT,       "Alt + Ctrl + Insert"},
    {KEY_ALT_CTRL_DELETE,       "Alt + Ctrl + Delete"},
    {KEY_ALT_CTRL_HOME,         "Alt + Ctrl + Home"},
    {KEY_ALT_CTRL_END,          "Alt + Ctrl + End "},
    {KEY_ALT_CTRL_PAGE_UP,      "Alt + Ctrl + Page Up"},
    {KEY_ALT_CTRL_PAGE_DOWN,    "Alt + Ctrl + Page Down"},
    
    {KEY_ALT_CTRL_SPACE,        "Alt + Ctrl + Space"},
};
/************************************************************************/
/*                                                                      */
/************************************************************************/
void printf_val(int key) {
    if(key < 32) {
        printf("%d %s\r\n", key, keyboard_name[key - KEY_UP].name);
    } else if(key < 128) {
        printf("%d %c\r\n", key, key);
    } else {
        printf("%d %s\r\n", key, keyboard_name[key - KEY_UP].name);
    }
    
    if(key == 'q')
        exit(0);
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
int main(int argc, char* argv[])
{
    ersh_keyboard_init(printf_val);
    while(1) {
        ersh_keyboard_poll();
    }
    
    return 0;
}